package com.lanou3g.myapplication.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou3g.myapplication.bean.BeanData;
import com.lanou3g.myapplication.R;
import com.lanou3g.myapplication.activity.singleton.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/7.
 */
public class RecommendedAdapter extends BaseAdapter {
    private List<BeanData> data;
    private Context context;
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;
    final int TYPE_3 = 2;
//  ImageLoader imageLoader;


    public RecommendedAdapter(List<BeanData> data, Context context) {
        this.data = data;
        this.context = context;
//        RequestQueue queue = Volley.newRequestQueue(context);
//        MemoryCache cache = new MemoryCache();
//        imageLoader = new ImageLoader(queue, cache);
        VolleySingleton.getInstance();
        Log.d("RecommendedAdapter", "data:" + data);

    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {

        return i;
    }

    @Override
    public int getItemViewType(int position) {
        int i = data.get(position).getItem_type();

        if (i == 1) {
            return TYPE_1;
        } else if (i == 0) {
            return TYPE_2;
        } else if (i == 2) {
            return TYPE_2;
        } else {
            return TYPE_3;
        }
    }


    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder1 viewHolder1 = null;
        ViewHolder2 viewHolder2 = null;
        ViewHolder3 viewHolder3 = null;

        int type = getItemViewType(position);
        String imageUrl = data.get(position).getImageUrl();
        if (convertView == null) {
            switch (type) {
                case TYPE_1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.listview1_item, parent, false);
                    viewHolder1 = new ViewHolder1(convertView);
                    convertView.setTag(viewHolder1);
                    break;
                case TYPE_2:
                    convertView = LayoutInflater.from(context).inflate(R.layout.listview2_item, parent, false);
                    viewHolder2 = new ViewHolder2(convertView);
                    convertView.setTag(viewHolder2);
                    break;
                case TYPE_3:
                    convertView = LayoutInflater.from(context).inflate(R.layout.listview3_item, parent, false);
                    viewHolder3 = new ViewHolder3(convertView);
                    convertView.setTag(viewHolder3);
                    break;
            }
        } else {
            switch (type) {
                case TYPE_1:
                    viewHolder1 = (ViewHolder1) convertView.getTag();
                    break;
                case TYPE_2:
                    viewHolder2 = (ViewHolder2) convertView.getTag();
                    break;
                case TYPE_3:
                    viewHolder3 = (ViewHolder3) convertView.getTag();
                    break;
            }
        }
        switch (type) {

            case TYPE_1:
                ImageLoader.ImageListener listener1 = VolleySingleton.getImageLoader().getImageListener(
                        viewHolder1.imageView1, R.mipmap.loading, R.mipmap.failure);
                VolleySingleton.getImageLoader().get(imageUrl, listener1, 300, 200, ImageView.ScaleType.FIT_XY);
                // viewHolder1.networkImageView1.setDefaultImageResId(R.mipmap.loading);
                // viewHolder1.networkImageView1.setErrorImageResId(R.mipmap.failure);
                viewHolder1.textView1.setText(data.get(position).getTitle());
                // viewHolder1.networkImageView1.setImageUrl(imageUrl, VolleySingleton.getImageLoader());
                break;
            case TYPE_2:
                ImageLoader.ImageListener listener2 = VolleySingleton.getImageLoader().getImageListener(
                        viewHolder2.imageView2, R.mipmap.loading, R.mipmap.failure);
                VolleySingleton.getImageLoader().get(imageUrl, listener2, 600, 570, ImageView.ScaleType.FIT_XY);
                // viewHolder2.networkImageView2.setDefaultImageResId(R.mipmap.loading);
                // viewHolder2.networkImageView2.setErrorImageResId(R.mipmap.failure);
                viewHolder2.textView2.setText(data.get(position).getTitle());
                // viewHolder2.networkImageView2.setImageUrl(imageUrl, VolleySingleton.getImageLoader());
                break;
            case TYPE_3:

                if (data.get(position).getTitle().length() > 0) {
                    viewHolder3.textView3.setText(data.get(position).getTitle());
                } else {
                    viewHolder3.textView3.setText(data.get(position).getSummary());
                }


        }

        return convertView;
    }

    class ViewHolder1 {
        TextView textView1;
        ImageView imageView1;

        public ViewHolder1(View view) {

            textView1 = (TextView) view.findViewById(R.id.listview1_item_tv);
            imageView1 = (ImageView) view.findViewById(R.id.listview1_item_iv);

        }
    }

    class ViewHolder2 {
        TextView textView2;
        ImageView imageView2;

        public ViewHolder2(View view) {
            textView2 = (TextView) view.findViewById(R.id.listview2_item_tv);
            imageView2 = (ImageView) view.findViewById(R.id.listview2_item_iv);

        }
    }

    class ViewHolder3 {
        TextView textView3;

        public ViewHolder3(View view) {
            textView3 = (TextView) view.findViewById(R.id.listview3_item_tv);

        }
    }
}
